using UnityEngine;
using System.Collections;

public class Hodor : BossCanTakeDamage 
{
	public Motor mMotor;
	
	
	public GameObject[] AI_Ships;
	
	public float spawnerRate = 3f;
	public float bulletFireRate = 3;
	public float rocketSpeed = .5f;
	public float rocketFireRate = 2;
	
	private float mTimerShipSpawn = 0;
	private float mTimerFireRate = 0;
	private float mTimerRocketTime = 0;
	
	public GameObject Bullet;


    public GUIStyle hodorStyle;

	public GameObject RocketPrefab;
	
	public static bool turnonwingui;
	
	private Transform mPlayer;
	private GameObject city;
	public GameObject explosion1;
	protected override void Start ()
	{
		city = GameObject.FindGameObjectWithTag("city").transform.GetChild(0).gameObject;

		mPlayer = GameObject.FindGameObjectWithTag("Player").transform;
		base.Start ();
//		SetValues();
		turnonwingui = false;
	}
	
	void Update()
	{
		Fire();
		SpawnShips();
		ShootRocket();
		mMotor.Move();
	}
	
	void SpawnShips()
	{
		if((mTimerShipSpawn += Time.deltaTime) >= spawnerRate)
		{

			mTimerShipSpawn = 0;
			GameObject clone = ((GameObject)Instantiate(AI_Ships[Random.Range(0, AI_Ships.Length)], transform.position, Quaternion.Euler(90,-180,0)));
			clone.gameObject.tag = "HodorSpawn";
			if(clone.transform.childCount > 0)
			{
				foreach(Transform child in clone.transform)
				{
					child.tag = child.parent.tag;
				}
			}
			//clone.transform.parent = gameObject.transform;
            
			//Spawner.waveEnemies++;
		}
	}
	
	void Fire()
	{
		if((mTimerFireRate += Time.deltaTime) >= bulletFireRate)
		{
			mTimerFireRate = 0;
			Weapon b = ((GameObject)Instantiate(Bullet,transform.position, Quaternion.Euler(270, 0, 0))).GetComponent<Weapon>();
            b.fireDirection = Vector3.Cross(new Vector3(270, 0, 0), Vector3.down).normalized;
		}
	}
	
	void ShootRocket()
	{
		if((mTimerRocketTime += Time.deltaTime) >= rocketFireRate)
		{
			mTimerRocketTime = 0;
			Rocket b = ((GameObject)Instantiate(RocketPrefab,transform.position,
                Quaternion.Euler(new Vector3(270+180, 90, 270)))).GetComponent<Rocket>();
			b.fireDirection = Vector3.down;
			b.destVector = transform.position - mPlayer.position;
			b.rocketSpeed = rocketSpeed;
		}
	}
	

	public override void TakeDamage(int dmg)
	{
		ToggleShader();

		SetValues();
		base.TakeDamage(dmg);
		Invoke("ToggleShader", .2f);
	}
	public void ToggleShader()
	{
		
		if (renderer.material.shader == Shader.Find("Transparent/Diffuse") || renderer.material.shader == Shader.Find("Transparent/Bumped Diffuse"))
		{
			//renderer.material.color = Color.red;
			renderer.material.shader = Shader.Find("Particles/Additive");
			
		}
		else if (renderer.material.shader == Shader.Find("Particles/Additive"))
		{
			//renderer.material.color = Color.white;
			print("go back");
			renderer.material.shader = Shader.Find("Transparent/Diffuse");
		}
	}
	
	//TODO Spawn more guys as he loses health
	void SetValues()
	{
		//percentage  = ((float) CurrHealth) / Health;
//		mCurrShipSpawnTime = Mathf.Lerp(ShipSpawnTimeStart, ShipSpawnTimeEnd, 10);
//		mCurrFireRate = Mathf.Lerp(FireRateStart, FireRateEnd, percentage);
		//mCurrRocketTime = Mathf.Lerp(RocketTimeStart, RocketTimeEnd, percentage);
	}
	public override void Die()
	{
		Instantiate( explosion1,transform.position,Quaternion.identity);
        PauseMenu.enemiesKilled++;
		base.Die();
		GameObject[] KILLME = GameObject.FindGameObjectsWithTag("HodorSpawn");
		foreach(GameObject kill in KILLME)
		{
			if(kill.gameObject.GetComponent<AIBase>()!=null)
			kill.gameObject.GetComponent<AIBase>().Die();
			if(kill.gameObject.GetComponent<orbit>()!=null)
				kill.gameObject.GetComponent<orbit>().Die();
		}
        if (PlayerPrefs.GetString("Diffculty") != "Boss")
        {
            city.SetActive(true);
        }
//		Spawner.waveEnemies = 0;
//		turnonwingui = true;
		//Time.timeScale = 0;
    }
    public void OnGUI()
    {
        GUI.Box(new Rect(Screen.width / 2 - 150, Screen.height / 2 + 380, 350, 60), "BOSS 1: " + CurrHealth.ToString(), hodorStyle);
    }
}
